
	/*
	 * Created on Feb 23, 2004
	 *
	 * To change the template for this generated file go to
	 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
	 */
	package ModuleBuilder.UI.tree;

	import java.util.*;
	import java.awt.event.ActionEvent;
	import java.awt.event.ActionListener;
	import javax.swing.*;
	import javax.swing.event.*;
	import javax.swing.BorderFactory; 
	import javax.swing.border.*;
	import javax.swing.tree.*;

	import java.awt.*;
	import java.awt.event.*;
	import java.io.*;
	import ModuleBuilder.*;
	import ModuleBuilder.model.*;	
	import ModuleBuilder.UI.*;
	import java.beans.*;

	/**
	 * @author Milton  Soong
	 *
	 * To change the template for this generated type comment go to
	 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
	 * 
	 * This is the unit editor class
	 * See page 59 of the notebook for the graphical layout of the whole panel
	 */
	public class OBEditor extends JPanel implements ActionListener, iEditorPanel {	
	
		private static final String CLEAR_CMD="Clear".intern();	// clear all the fields w/o saving
		private static final String SAVE_CMD="Save".intern();	// save and create a new node (if does not exist previously) with current data
		private static final String SAVE_SUB_CMD="Save Sub".intern();
		private static final String COPY_CMD="Copy".intern();	// copy and make a new unit type based on the selected one																// this is NOT valid on the main editor window
		private static final String ADD_CMD="Add Stand".intern();	// add a new attribute to this
		private static final String DELETE_CMD="Delete".intern();	// this is NOT valid on the main editor window
		private static final String PROFILE_CMD="Profile".intern();
		private static final String SIDEVIEW_CMD="Pick Side View".intern();
		public static final String DEFAULT_NAME="default".intern(); 
		
		public static final String WHOLE_PROFILE="whole unit profile".intern();
		public static final String STAND_PROFILE="indiv stand profile".intern();
		
		private static final String PROFILE_RESULT_UNIT="unit".intern();	// display panel for whole unit profile
		private static final String PROFILE_RESULT_STAND="std".intern();	// display panel for individual stand profile
		
	
		private JTextField unTextField;	// the user given name of the unit "name", i.e. "Greek Hopelite No.1".
		ArrayList stands;		// an array list that holds a bunch of StandPanel.
		JPanel allStandsPanel;	// a panel that can grow or shrink in size to contain each unit type attribute
		private JScrollPane allStandsScrollPane; 
		private UnitModel model;	// the Current model being operated at.
		private GameTreeEditor gameTreeEditor;	// the parent
		private JCheckBox isCommandCB;		// checked if this is a top level container	
	
		JButton clearButton;
		JButton saveButton;
		JButton saveSubButton;
		JButton copyButton;
		JButton addStandButton;
		JButton deleteButton;
		JButton sideViewButton;
			
		JComboBox profileListCB;	// used for the unit to select the unit profile
		HashMap	profileHash;		// a hashMap of all profile name --> actual item (so dups can be deleted)
		
		BuilderTrayModel sideViewTrayMod, topViewTrayMod;
		
		JRadioButton	wholeUnitProB, standProB;
		ButtonGroup  	profileChoiceGroup;		
		CardLayout		profileChoiceCl;	
		JPanel 			profileResultPanel;	
		JPanel 			buttonPanel;	
		AttribPanel 	attribInputP;	// where the unit attribute input panel is located.
		JScrollPane		attribInputScrollPanel;	// the scroll pane where the attribInputP is located in
		
		ImagePreview figureSideView = null;
		
		/**
		 * Hashmap that contains a button object ==> StandPanel lookup
		 * This data structure needs to be reset everytime a new model is set into the UI
		 * and we'll have to build the hashMap from scratch.
		 */
		HashMap standPanelHash;
	
		public OBEditor(GameTreeEditor in) {
			gameTreeEditor = in;
			standPanelHash = new HashMap();
			
			sideViewTrayMod = new BuilderTrayModel();
			topViewTrayMod = new BuilderTrayModel();
			
			stands = new ArrayList();
			JLabel unLabel = new JLabel("Unit Name: ");
			unTextField = new JTextField();
			isCommandCB = new JCheckBox("Command");
			isCommandCB.setSelected(false);

			// First setup the Unit Layout on top
			JPanel unitNameInputPanel = new JPanel();
			unitNameInputPanel.setLayout(new BoxLayout(unitNameInputPanel, BoxLayout.LINE_AXIS));
			unitNameInputPanel.add(unLabel);
			unitNameInputPanel.add(unTextField);
			unitNameInputPanel.add(Box.createRigidArea(new Dimension(10, 0)));
		
			// now need to set some size limitations to the components
			Dimension d = unTextField.getPreferredSize();
			d.width = 200;
			unTextField.setMinimumSize(d);
			unTextField.setPreferredSize(d);
			unTextField.setMaximumSize(d);

			// Next the main body which is enclosed inside of a scrollable panel
			// the mainbody always contains a Grid of AttributePanel
			//
			allStandsPanel = new JPanel();
			allStandsPanel.setLayout(new BoxLayout(allStandsPanel, BoxLayout.PAGE_AXIS));
		
			allStandsScrollPane = new JScrollPane(allStandsPanel);
		
			// Create the Button panel in the bottom
			setupButtonPanel();
		
			// setup the side view selection button as well as the side view tray
			sideViewButton = new JButton(SIDEVIEW_CMD);
			sideViewButton.setActionCommand(SIDEVIEW_CMD);
			sideViewButton.addActionListener(this);
			figureSideView = new ImagePreview(this, "Unit Side View");
			figureSideView.setAlignmentX(JComponent.LEFT_ALIGNMENT);
			figureSideView.setAlignmentY(JComponent.TOP_ALIGNMENT);
			JPanel rightP = new JPanel();
			rightP.setLayout(new BoxLayout(rightP, BoxLayout.PAGE_AXIS));
			figureSideView.setAlignmentX(JComponent.CENTER_ALIGNMENT);
			rightP.add(figureSideView);
			sideViewButton.setAlignmentX(JComponent.CENTER_ALIGNMENT);
			rightP.add(sideViewButton);		
			
			TerrainTray sideViewTray = new TerrainTray("Figure Side View", TerrainTray._FIGURE_SIDEVIEW);
			TerrainTray topViewTray = new TerrainTray("Figure Top View", TerrainTray._FIGURE_TOPVIEW);

			// Now setup the profileChoicePanel
			wholeUnitProB = new JRadioButton("Whole unit uses one profile");
			wholeUnitProB.setActionCommand(WHOLE_PROFILE);
			wholeUnitProB.setSelected(true);
			
			standProB = new JRadioButton("Individual stand uses own profile");
			standProB.setActionCommand(STAND_PROFILE);
			standProB.setEnabled(false);		// !!! TBD
			
			profileChoiceGroup = new ButtonGroup();
			profileChoiceGroup.add(wholeUnitProB);
			profileChoiceGroup.add(standProB);
			
			JPanel profileChoicePanel = new JPanel();
			profileChoicePanel.setLayout(new BoxLayout(profileChoicePanel, BoxLayout.PAGE_AXIS));
			profileChoicePanel.add(wholeUnitProB);
			profileChoicePanel.add(standProB);
			profileChoicePanel.add(isCommandCB);
			
			// Now setup the topLeftPanel that contains the unitName inoput and the 
			// profile type chooser
			//
			JPanel topLeftPanel = new JPanel();
			topLeftPanel.setLayout(new BoxLayout(topLeftPanel, BoxLayout.LINE_AXIS));
			unitNameInputPanel.setAlignmentY(JComponent.TOP_ALIGNMENT);
			topLeftPanel.add(unitNameInputPanel);
			profileChoicePanel.setAlignmentY(JComponent.TOP_ALIGNMENT);
			topLeftPanel.add(profileChoicePanel);

			// Setup the UnitProfilePanel
			setupProfileResultPanel();
			
			// now setup the panel left
			JPanel leftP = new JPanel();
			leftP.setLayout(new BoxLayout(leftP, BoxLayout.PAGE_AXIS));
			unitNameInputPanel.setAlignmentX(JComponent.LEFT_ALIGNMENT);
			// profileResultPanel.setAlignmentX(JComponent.LEFT_ALIGNMENT);
			leftP.add(topLeftPanel);
			leftP.add(Box.createRigidArea(new Dimension(0, 10)));
			leftP.add(profileResultPanel);

			// Now do the Top Panel
			JPanel topPanel = new JPanel();
			topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.LINE_AXIS));
			leftP.setAlignmentY(JComponent.TOP_ALIGNMENT);
			rightP.setAlignmentY(JComponent.TOP_ALIGNMENT);
			topPanel.add(leftP);
			topPanel.add(Box.createRigidArea(new Dimension(5,0)));
			topPanel.add(rightP);
			
			// now do the Grand layout of the whole panel
			this.setLayout( new BoxLayout(this, BoxLayout.PAGE_AXIS));
			this.add(topPanel);
			// allStandsScrollPane.setAlignmentX(JComponent.LEFT_ALIGNMENT);
			this.add(allStandsScrollPane);
			this.add(Box.createVerticalGlue());
			// buttonPanel.setAlignmentX(JComponent.LEFT_ALIGNMENT);
			this.add(buttonPanel);
			Color bc = this.getBackground();
			this.setBorder(BorderFactory.createMatteBorder(10,10,10,10, bc));
		}
		
		private void setupButtonPanel() {
			clearButton = new JButton(CLEAR_CMD);
			clearButton.setActionCommand(CLEAR_CMD);
			clearButton.addActionListener(this);
 
			saveButton = new JButton(SAVE_CMD);
			saveButton.setActionCommand(SAVE_CMD);
			saveButton.addActionListener(this);
 
			saveSubButton = new JButton(SAVE_SUB_CMD);
			saveSubButton.setActionCommand(SAVE_SUB_CMD);
			saveSubButton.addActionListener(this);
 
			copyButton = new JButton(COPY_CMD);
			copyButton.setActionCommand(COPY_CMD);
			copyButton.addActionListener(this);
 
			addStandButton = new JButton(ADD_CMD);
			addStandButton.setActionCommand(ADD_CMD);
			addStandButton.addActionListener(this);

			deleteButton = new JButton(DELETE_CMD);
			deleteButton.setActionCommand(DELETE_CMD);
			deleteButton.addActionListener(this);
 	 	
			buttonPanel = new JPanel();
			buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
			buttonPanel.add(addStandButton);
			buttonPanel.add(copyButton);
			buttonPanel.add(clearButton);
			buttonPanel.add(deleteButton);
			buttonPanel.add(saveButton);
			buttonPanel.add(saveSubButton);
		}
		
		private void setupProfileResultPanel() {
			// now we need to initialize the Unit Profile picker
			profileListCB = new JComboBox();
			profileHash = new HashMap();
			profileListCB.setActionCommand(PROFILE_CMD);
			profileListCB.addActionListener(this);
			Dimension d = profileListCB.getPreferredSize();
			d.width = 250;
			profileListCB.setMaximumSize(d);
			profileListCB.setMinimumSize(d);
			profileListCB.setPreferredSize(d);
			
			JLabel profileListLabel = new JLabel("Unit Profiles:");
			JPanel unitPInput = new JPanel();
			unitPInput.add(profileListLabel);
			unitPInput.add(profileListCB);
			
			// setup an Attribute Panel
			attribInputP = new AttribPanel();	
			attribInputScrollPanel = new JScrollPane(attribInputP);
			
			JPanel unitProfileP = new JPanel();
			unitProfileP.setBorder(BorderFactory.createEtchedBorder());
			
			unitProfileP.setLayout(new BoxLayout(unitProfileP, BoxLayout.PAGE_AXIS));
			unitPInput.setAlignmentX(JComponent.LEFT_ALIGNMENT);			
			unitProfileP.add(unitPInput);
			attribInputP.setAlignmentX(JComponent.LEFT_ALIGNMENT);
			unitProfileP.add(attribInputScrollPanel);
			unitProfileP.add( Box.createVerticalGlue());
			
			// Setup A Blank individual Stand panel
			JPanel standP = new JPanel();
			
			/* Next setup the unit profile panel which is going to be a card
			** layout controlled by the Profile chooser
			*/			
			profileResultPanel = new JPanel();
			profileChoiceCl = new CardLayout();
			profileResultPanel.setLayout(profileChoiceCl);
			profileResultPanel.add(unitProfileP, PROFILE_RESULT_UNIT );
			profileResultPanel.add(standP, PROFILE_RESULT_STAND );
			profileChoiceCl.show(profileResultPanel, PROFILE_RESULT_UNIT);
			
			// setup the control to show the card layout
			//
			wholeUnitProB.addActionListener(this);
			standProB.addActionListener(this);

			return;
		}
	
		protected void populateProfileList() {
			Object[] profileList= gameTreeEditor.getProfileList();
			for (int i=0; i< profileList.length; i++) {
				newProfileAdded(profileList[i]);
			}
			//handleProfileChange();
			return;
		}
		
		/**
		 * Need to update the combo box list list
		 * @param in the actual DefaultMutableTreeNode of the profile
		 */
		protected void newProfileAdded(Object in) {
			DefaultMutableTreeNode node = (DefaultMutableTreeNode)in;
			UnitProfileModel profile = (UnitProfileModel)node.getUserObject();
			
			/*
			 * check if there's another profile by the same name,
			 * if there is then remove it from the profileListCB first
			 */
			String name = profile.getName();
			UnitProfileModel oldProfile = (UnitProfileModel)profileHash.get(name);
			if ( oldProfile != null ) {
				// There is already a dup, delete it from hash and the CB
				profileHash.remove(oldProfile);
				profileListCB.removeItem(oldProfile);
			}
			profileHash.put(name, profile);
			profileListCB.addItem(profile);
			handleProfileChange();
		}
		
		/**
		 * The said object has been taken away. need to 
		 * 1. take it away from the ComboBox, then
		 * 2. If this is currently used as the selected profile, then we need to 
		 * 		clear out all the stands. 
		 */
		protected void profileDeleted(Object in) {
			DefaultMutableTreeNode node = (DefaultMutableTreeNode)in;
			UnitProfileModel profile = (UnitProfileModel)node.getUserObject();

			profileHash.remove(profile);
			profileListCB.removeItem(profile);
			handleProfileChange();
		}
		
		// this needs to be called when all the views are added in from a scenario load
		protected void figureSideViewAdded(ImageElement el) {
			ImageIcon thumbnailIcon = el.getSmallIcon();
			sideViewTrayMod.addElement(el);
			TerrainTray t = (TerrainTray)TerrainTray.lookup.get(TerrainTray._FIGURE_SIDEVIEW);
			t.setModel(sideViewTrayMod);
			
		}
		
		protected void figureTopViewAdded( ImageElement el ) {
			ImageIcon thumbnailIcon = el.getSmallIcon();
			topViewTrayMod.addElement(el);
			TerrainTray t = (TerrainTray)TerrainTray.lookup.get(TerrainTray._FIGURE_TOPVIEW);
			t.setModel(topViewTrayMod);
		}

		/**
		 * Given an input of a model, get all the data and display it in the panel
		 * @param model
		 */
		public void setModel( GameModel in ) {
			model = (UnitModel)in;
			handleClearCmd();
			if ( model != null ) {
				deleteButton.setEnabled(true);
				copyButton.setEnabled(true);

				// an existing unit has just been set in here, need to populate the UI with what's in the model
				String name = model.getName();
				unTextField.setText(name);
				boolean isCommand = model.isCommand();
				isCommandCB.setSelected(isCommand);
				
				// setup the side view
				ImageElement sideImage = model.getSideImage();
				figureSideView.imageChanged(sideImage);

				
				UnitProfileModel profile = model.getProfileModel();
				int standType=StandPanel._SIMPLE_STAND;
				if ( profile != null) {
					// the profile applies to the whole unit
					ComboBoxModel m = profileListCB.getModel();
					m.setSelectedItem(profile);
					loadModelIntoUnitProfilePanel(profile);

				} else {
					standType=StandPanel._COMPLEX_STAND;
				}
				
				// loop through all the stands and create the StandPanel
				ArrayList loclist = model.getStands();
				for ( int i=0; i < loclist.size(); i++ ){
					Stand currStand = (Stand)loclist.get(i);
					StandPanel standPanel = StandPanel.load(currStand, standType, this);
					allStandsPanel.add(standPanel);
					stands.add(standPanel);
					
					// for ea StandP, setup the button to panel lookup relationship
					JButton deleteB = standPanel.getDeleteB();
					standPanelHash.put(deleteB, standPanel);
				}
				allStandsPanel.validate();
			} else {
				// if model is null that indicates this is a editor node
				deleteButton.setEnabled(false);
				copyButton.setEnabled(false);
			}
			this.validate();
			this.repaint();
			return;
		}
	
		/**
		 * Given 
		 * @return
		 */
		public UnitModel getModel(){
			return( model );
		}
	
		/**
		 * Depending on which button is clicked, do the right thing.
		 */	
		public void actionPerformed(ActionEvent e) {
			String command = e.getActionCommand();
    
			if (CLEAR_CMD.equals(command)) {
				handleClearCmd();
			
			} else if (SAVE_CMD.equals(command)) {
				handleSaveCmd();
			
			} else if (SAVE_SUB_CMD.equals(command)) {
				handleSaveSubCmd();
			
			} else if (COPY_CMD.equals(command)) {
				handleCopyCmd();
			
			} else if (ADD_CMD.equals(command)) {
				handleAddStandCmd();
				
			} else if (DELETE_CMD.equals(command)) {			
				handleDeleteCmd();
			
			} else if (PROFILE_CMD.equals(command)) {			
				handleProfileChange();
				
			} else if (SIDEVIEW_CMD.equals(command)) {
				showTray(TerrainTray._FIGURE_SIDEVIEW, this.figureSideView);
				
			} else if (WHOLE_PROFILE.equals(command)) {
				handleWholeUnitProfileSelected();
				
			} else if (STAND_PROFILE.equals(command)) {
				handleStandProfileSelected();
			}
		}
	
		protected void handleClearCmd() {
			// delete all the stands from the UI
			// 1) loop through all the attribPanel from the stands ArrayList,
			// 2) for ea item, remove from the container, and from the ArrayList  
			//
			unTextField.setText(null);
			allStandsPanel.removeAll();
			stands.clear();
			// (DO NOT REMOVE ALL, since we need to repopulate it back to the correct value attribInputP.removeAll();
			// DOES NOT WORK model=null;
			this.repaint();
		}

		/**
		 * If there already is a model, then we'll just write over what's in it.
		 * If there isn't currently a model, then we'll create a new one.
		 *
		 */
		protected void handleSaveCmd(){
			boolean isNew = false;
			if ( model == null ) {
				model = new UnitModel(null, GameModel.OB_EDITOR, false, this);
				isNew = true;
			} else {
				// remove all the old stands 
				model.removeAllStands();
			}
			storeModelFromUI( stands, model );
		
			if ( isNew ) {
				gameTreeEditor.nodeCreated(model);
				handleClearCmd();
			} else {
				// reload in case the name have changed
				// COMMENT OUT TO TRY NO CLOSE gameTreeEditor.reloadTreeModel();
				gameTreeEditor.nodeChanged();
			}
			//model = null;
			return;
		}
		
		/**
		 * This will cause the save to occur except will Always save a new node as 
		 * a subordinate 
		 */
		protected void handleSaveSubCmd() {
			UnitModel localModel = new UnitModel(null, GameModel.OB_EDITOR, false, this);
			storeModelFromUI( stands, localModel );
			gameTreeEditor.nodeCreated(localModel);
			handleClearCmd();
			// DO NOT ZERO OUT THE CURRENT MODEL
			// model = null;
			return;
		}
		
		private void handleWholeUnitProfileSelected() {
			profileChoiceCl.show(profileResultPanel, PROFILE_RESULT_UNIT);
		}
				
		private void handleStandProfileSelected() {			
			profileChoiceCl.show(profileResultPanel, PROFILE_RESULT_STAND);
		}

		/**
		 * Take what's in the UI and move it into the model
		 *
		 */
		private void storeModelFromUI(ArrayList uiList, UnitModel localMod) {
			String name = unTextField.getText();
			if (name == null) {
				name = DEFAULT_NAME;
			}
			localMod.setCommand(isCommandCB.isSelected());
			localMod.setName(name);
			
			// save the side view
			ImageElement el = figureSideView.getImageElement();
			localMod.setSideImage(el);

			// if the "profile applies to whole unit" is selected, then save the attributes
			if ( wholeUnitProB.isSelected() == true ){
				//DefaultMutableTreeNode node = (DefaultMutableTreeNode)profileListCB.getSelectedItem();
				UnitProfileModel profile = (UnitProfileModel)profileListCB.getSelectedItem();
				localMod.setProfileModel(profile);
				
				// now figure out if any of the attributesPanel contains a range, and if so, record the 
				// current value for that range.
				//
				Component[] children = attribInputP.getComponents();
				if ( children != null ) {
					localMod.clearHashValue();
					for ( int i=0; i < children.length; i++ ){
						if ( children[i] instanceof AttribValuePanel.RangeAttribValuePanel ) {
							AttribValuePanel.RangeAttribValuePanel valP = (AttribValuePanel.RangeAttribValuePanel)children[i];
							Object res = valP.getObject();
							String rangeName = valP.getName();
							localMod.addHashValue(rangeName, res);
						}
					}
				}
			}
			
			// now loop through ea stand panel and create an object in each.
			//
			for ( int i=0; i < uiList.size(); i++ ){
				StandPanel standP = (StandPanel)uiList.get(i);
				Stand standModel = standP.store();				
				localMod.addStand(standModel);
			}
			return;		
		}

		/**
		 * Assume that if we get here, this has to be a leaf node
		 * 1. clear the UI, 
		 * 2. then call gameTreeEditor to delete this node
		 */
		protected void handleDeleteCmd() {
			handleClearCmd();
			gameTreeEditor.deleteCurrentNode();	
		}
	
		protected void handleCopyCmd(){
			boolean isNew = true;
		
			UnitModel copyModel = new UnitModel(null, GameModel.OB_EDITOR, false, this);
			storeModelFromUI( stands, copyModel );
			String name = unTextField.getText();
			if (name != null) {
				// add "copy" to the end of the name
				name += "_copy";
			}
			copyModel.setName(name);		
			gameTreeEditor.siblingNodeCreated(copyModel);
			return;
		}
	
		protected void handleAddStandCmd() {
			// Create a new Attribute panel, and then add it to the allStandsPanel
			StandPanel standPanel = StandPanel.create(StandPanel._SIMPLE_STAND, this);
			allStandsPanel.add(standPanel);
			stands.add(standPanel);
			this.validate();
			return;
		}
	
		/**
		 * Important, this means that the user has changed the selection of the profile
		 * When the selected profile changes:
		 * 1) get the current selected profile object, 
		 * 2) Create the attribPanel with what's in the profile object.
		 */
		protected void handleProfileChange() {
			UnitProfileModel profile = (UnitProfileModel)profileListCB.getSelectedItem();
			// UnitProfileModel profile = (UnitProfileModel)node.getUserObject();
			loadModelIntoUnitProfilePanel(profile);
		}
		
		/*
		 * 
		 * @author Milton  Soong
		 *
		 * this is called when:
		 * 1) Selected profile has changed, and need to repaint the UNit Profile Panel, or
		 * 2) when new model needs to be loaded into the UI panel 
		 * 3) A profile previously in use has been deleted, need to cleanup
		 */ 		
		private void loadModelIntoUnitProfilePanel(UnitProfileModel profile) {
			if ( profile != null ) {
				ArrayList attributes = profile.getAttributes();
				attribInputP.removeAll();	// take all the individual attrib panel out
				for (int i=0; i< attributes.size(); i++) {
					UnitProfileAttribute unitAttrib = (UnitProfileAttribute)attributes.get(i);
					AttribValuePanel attribValP = AttribValuePanel.create(unitAttrib);
					attribInputP.add(attribValP);
					// if this is a range type, then we will need to set the init value to what's in the model
					Object attribObj = unitAttrib.getVal();
					if ( (attribObj instanceof RangeUnitProfileAttribute) && 
						 (model != null) ) {
						AttribValuePanel.RangeAttribValuePanel locRange = (AttribValuePanel.RangeAttribValuePanel)attribValP;
						Object rangeVal = model.getHashValue(unitAttrib.getName());
						if ( rangeVal instanceof Integer ) {
							locRange.setObject(rangeVal);
						} else {
							// from a previous rev (pre 9/20/04) where init value is not saved
							// in which case we need to use the init value from a fresh profile
							RangeUnitProfileAttribute profAttr = (RangeUnitProfileAttribute)attribObj;
							locRange.setObject(profAttr.getInitVal());
						}
						
					}
				}
				attribInputP.add( Box.createVerticalGlue());
				attribInputP.repaint();
			} else {
				// there are no profile selected, so that means a previously selected profile 
				// has been deleted.
				Debug.out("loadModelIntoUnitProfilePanel: profile in == null");
			}
			return;			
		}
		
		private final static String treeNodeString ="OB Editor".intern();
		public String toString() {
			return (treeNodeString);
		}
	
//		public void showTray(String id) {
//		  TerrainTray currTray = (TerrainTray)TerrainTray.lookup.get(id);
//		  Dimension dlgSize = currTray.getPreferredSize();
//		  Dimension frmSize = getSize();
//		  Point loc = getLocation();
//		  // currTray.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
//		  // currTray.pack();
//		  currTray.show();
//		}

		public void showTray(String id, iImagePreviewChangeListener listener ) {
		  TerrainTray currTray = (TerrainTray)TerrainTray.lookup.get(id);
		  Dimension dlgSize = currTray.getPreferredSize();
		  Dimension frmSize = getSize();
		  Point loc = getLocation();
		  currTray.setSize(dlgSize);
		  // currTray.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
		  // currTray.pack();
		  currTray.setImagePreviewChangeListener(listener);
		  currTray.show();
		}
		
		/**
		 * 
		 * @author Milton  Soong
		 *
		 * This class handles the user profiles that's displayed for this particular 
		 * instance of the unit profile
		 */
		public class AttribPanel extends JPanel {
			public AttribPanel() {
				super();
				this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
			}
		}
	
	}
